package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.adapter.BankAdapter;
import com.pengcheng.taskapplication.adapter.WalletBankAdapter;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.helper.ImageLoader;
import com.pengcheng.taskapplication.model.BankListItemModel;
import com.pengcheng.taskapplication.model.BankListModel;
import com.pengcheng.taskapplication.model.WalletModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenter;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.mvp.view.widget.MyListView;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.rxbus.RxBus;
import com.pengcheng.taskapplication.rxbus.RxBusRoute;
import com.pengcheng.taskapplication.rxbus.RxEvent;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ZFT on 2020/10/27.
 */

public class MyWalletBankList extends BaseActivity implements IView{
    private UserPresenterV2 userPresenterV2;
    @BindView(R.id.pull_to_refresh_lv)
    MyListView pullToRefreshListView;
    @BindView(R.id.img_show)
    ImageView img_show;
    @BindView(R.id.tv_center_title)
    TextView tv_center;

    @BindView(R.id.ll_show_nodata)
    LinearLayout ll_show_nodata;

    @BindView(R.id.show_data)
    LinearLayout show_data;


    private String newtoken;
    private String uid;



    ArrayList<BankListItemModel> list =new ArrayList<>();
    private WalletBankAdapter adapter;
    @Override
    protected int layoutResID() {
        return R.layout.activity_my_wallet_bank_list_layout;
    }

    @Override
    protected void initData() {
        tv_center.setText("我的钱包");
        userPresenterV2 = new UserPresenterV2(this);
        uid= SPUtils.getString(context,ConstantValue.SpType.userid);
        newtoken=SPUtils.getString(context,ConstantValue.SpType.newtoken);
        //adapter
        adapter = new WalletBankAdapter(context,list);
        //manager
        pullToRefreshListView.setAdapter(adapter);

        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                BankListItemModel bankListItemModel=(BankListItemModel)adapterView.getAdapter().getItem(i);
                if(bankListItemModel!=null){
                    Intent intent =new Intent(context,UnbindBank.class);
                    intent.putExtra("model",bankListItemModel);
                    startActivity(intent);
                }
            }
        });

        getBankList(newtoken,uid);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.getBankList)){
            Gson gson= new Gson();
            Type type =new TypeToken<BankListModel>(){}.getType();
            BankListModel bankListModel= gson.fromJson(gson.toJson(data),type);





            if(bankListModel.getList().size()==0){
                //做页面跳转，跳转到添加页面

                ll_show_nodata.setVisibility(View.VISIBLE);
                show_data.setVisibility(View.GONE);



            }else {
                ll_show_nodata.setVisibility(View.GONE);
                show_data.setVisibility(View.VISIBLE);
                list.clear();
                list.addAll(bankListModel.getList());
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        ToastUtils.showShortToast(msg);
    }

    @OnClick({R.id.img_back,R.id.rl_add_bank,R.id.ll_add_bank_nodata})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
                //添加绑卡
            case R.id.rl_add_bank:
                Intent intent =new Intent(context,ApplyBindCard.class);
                startActivity(intent);
                break;
            case R.id.ll_add_bank_nodata:
                //跳转到添加银行卡
                Intent intent2 =new Intent(context,ApplyBindCard.class);
                startActivity(intent2);
                break;
        }
    }
    public void getBankList(String newtoken,String uid){
        userPresenterV2.getBankList(newtoken,uid,ConstantValue.RequestKey.getBankList);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected boolean isRegistRxBus() {
        return true;
    }

    /**
     * 注册订阅事件
     */
    protected void receiveEvent(RxEvent rxEvent) {
        switch (rxEvent.busName) {
            case RxBusRoute.LOGIN_OUT:
//                //退出登录
//                //userid  //nick  //avatar  //recode
//                SPUtils.getInstance().remove(ConstantValue.SpType.userid);
//                SPUtils.getInstance().remove(ConstantValue.SpType.recode);
//                SPUtils.getInstance().remove(ConstantValue.SpType.newavatar);
//                startActivity(new Intent(context, LoginActivity.class));
//                finish();
                break;
            case RxBusRoute.unbind:
                //刷新列表
                getBankList(newtoken,uid);
                break;


        }
    }
}
